package cn.exrick.manager.service.impl;

import cn.exrick.common.constant.CommonConstant;
import cn.exrick.manager.mapper.TbEmailTemplateMapper;
import cn.exrick.manager.mapper.TbMemberMapper;
import cn.exrick.manager.mapper.TbOrganizationMapper;
import cn.exrick.manager.pojo.*;
import cn.exrick.manager.service.EmailTemplateService;
import cn.exrick.manager.service.OrderService;
import cn.exrick.manager.service.SysSettingService;
import cn.exrick.manager.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EmailTemplateServiceImpl implements EmailTemplateService {

    @Autowired
    private TbEmailTemplateMapper tbEmailTemplateMapper;
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private OrderService orderService;
    @Autowired
    private TbOrganizationMapper tbOrganizationMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private TbMemberMapper tbMemberMapper;
    @Autowired
    private SysSettingService sysSettingService;

    @Override
    public void addTemplate(TbEmailTemplate tbEmailTemplate) {
        tbEmailTemplate.setCreateTime(new Date());
        tbEmailTemplate.setUpdateTime(new Date());
        tbEmailTemplateMapper.insert(tbEmailTemplate);
    }

    @Override
    public TbEmailTemplate getTemplateByType(Integer type) {
        TbEmailTemplateExample example = new TbEmailTemplateExample();
        TbEmailTemplateExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(type);
        List<TbEmailTemplate> tbEmailTemplates = tbEmailTemplateMapper.selectByExampleWithBLOBs(example);
        if(CollectionUtils.isEmpty(tbEmailTemplates)){
            return null;
        }
        return tbEmailTemplates.get(0);
    }

    @Override
    public void updateTemplate(TbEmailTemplate tbEmailTemplate) {
        tbEmailTemplate.setUpdateTime(new Date());
        tbEmailTemplateMapper.updateByPrimaryKeySelective(tbEmailTemplate);

    }

    @Override
    public void sendEmail(String subject, String sendTo, String content) {
        emailUtil.sendEmail(subject,sendTo,content);
    }

    @Override
    public void sendOrderApprovalEmail(String orderId, int type) {
        try {
            TbOrder tbOrder = orderService.getTbOrderById(orderId);
            TbEmailTemplate emailTemplae = getTemplateByType(CommonConstant.EMAIL_REMIND_TYPE_APPROVAL_ORDER);
            String template = emailTemplae.getTemplate();
            TbMember tbMember = tbMemberMapper.selectByPrimaryKey(tbOrder.getUserId());
            if(template.contains("${提交审批人名称}")){
                template = template.replace("${提交审批人名称}",tbMember.getUsername());
            }
            if(template.contains("${订单号}")){
                template = template.replace("${订单号}",tbOrder.getOrderNo());
            }
            String subject = "纪念品商城订单审批通知";
            if(type == 0){
                TbOrganization organization = tbOrganizationMapper.selectByPrimaryKey(tbMember.getOrganizationId());
                TbUser tbUser = userService.getUserById(organization.getChargeUserId());
                if(tbUser != null && StringUtils.isNotBlank(tbUser.getEmail())){
                    if(template.contains("${收件人名称}")){
                        template = template.replace("${收件人名称}",tbUser.getUsername());
                    }
                    sendEmail(subject,tbUser.getEmail(),template);
                }
            }else{
                List<TbUser> adminList = userService.getAllAdmin();
                for(TbUser tbUser : adminList){
                    if(StringUtils.isNotBlank(tbUser.getEmail())){
                        String content = template;
                        if(content.contains("${收件人名称}")){
                            content = content.replace("${收件人名称}",tbUser.getUsername());
                        }
                        sendEmail(subject,tbUser.getEmail(),content);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void sendPickupEmail(String orderId) {
        TbOrder tbOrder = orderService.getTbOrderById(orderId);
        TbMember tbMember = tbMemberMapper.selectByPrimaryKey(tbOrder.getUserId());
        if(StringUtils.isNotBlank(tbMember.getEmail())){
            TbEmailTemplate emailTemplae = getTemplateByType(CommonConstant.EMAIL_REMIND_TYPE_PICKUP);
            String template = emailTemplae.getTemplate();

            if(template.contains("${收件人名称}")){
                template = template.replace("${收件人名称}",tbMember.getUsername());
            }
            if(template.contains("${订单号}")){
                template = template.replace("${订单号}",tbOrder.getOrderNo());
            }
            if(template.contains("${取货码}")){
                template = template.replace("${取货码}",tbOrder.getCode());
            }
            if(template.contains("${取货有效时间}")){
                TbSysSetting setting = sysSettingService.getSetting();
                template = template.replace("${取货有效时间}",String.valueOf(setting.getPickupEffectiveTime()));
            }
            sendEmail("纪念品商城取货通知",tbMember.getEmail(),template);
        }
    }
}
